package com.github.shiqiyue.blog.markdown.config.session;

import org.redisson.spring.session.config.EnableRedissonHttpSession;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.DefaultCookieSerializer;

import com.github.shiqiyue.blog.markdown.config.ApplicationConfig;

/***
 * spring session配置
 *
 * @author wwy
 *
 */
@EnableConfigurationProperties(ApplicationConfig.class)
@Configuration
@EnableRedissonHttpSession
public class SpringSessionConfiguration {
	
	private SessionConfig sessionConfig;
	
	public SpringSessionConfiguration(ApplicationConfig applicationConfig) {
		this.sessionConfig = applicationConfig.getSession();
	}
	
	/****
	 * 配置session策略
	 *
	 * @return
	 */
	@Bean
	public CookieAndHeaderHttpSessionStrategy httpSessionStrategy() {
		DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
		cookieSerializer.setCookieName(sessionConfig.getCookieName());
		cookieSerializer.setCookieMaxAge(sessionConfig.getMaxAge());
		if (sessionConfig.isRememberMeEnable()) {
			cookieSerializer.setRememberMeRequestAttribute(sessionConfig.getRememberMeRequestAttribute());
		}
		CookieAndHeaderHttpSessionStrategy sessionStrategy = new CookieAndHeaderHttpSessionStrategy();
		sessionStrategy.cookieHttpSessionStrategy.setCookieSerializer(cookieSerializer);
		return sessionStrategy;
	}
	
}
